package com.dianrui.qiyouriding.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.event.RfreshPersionInfos;
import com.dianrui.qiyouriding.util.NoDoubleClickUtils;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

;

/**
 * 实名认证
 */
@SuppressWarnings("ALL")
public class AuthActivity extends BaseActivity {

    private String isBackAuth;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.card)
    EditText card;


    @Override
    public int getContentViewId() {
        return R.layout.activity_auth;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle=getIntent().getExtras();
            if (bundle != null) {
                isBackAuth = bundle.getString("isBackAuth");
            }

        }
        title.setText(getString(R.string.auth_title));
        customInit(true, R.color.main_color);
        card.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 18) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(card.getWindowToken(), 0);
                    }
                }
            }
        });

    }

    @Override
    public void initData() {}


    @OnClick({R.id.back, R.id.click_submit})
    public void onViewClicked(View view) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.back:
                if ("1".equals(isBackAuth)) {
                    finish();
                } else {
                    JumpActivitys(MainActivity.class);
                    finish();
                }
                break;
            case R.id.click_submit:
                if (StringUtils.isEmpty(name.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.auth_txt2));

                }else if (StringUtils.isEmpty(card.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.auth_txt3));

                } else if (card.getText().toString().length() < 18) {
                    ToastUtil.showToast(getString(R.string.auth_idcard_error));

                }else {
                    startAuth();
                }
                break;


            default:
                break;
        }
    }

    //开始认证
    private void startAuth() {
        JsonObject authJson = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            authJson.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        authJson.addProperty("name", name.getText().toString().trim());
        authJson.addProperty("card", card.getText().toString().trim());
        OkHttpRequest.getInstance().postRequests(Url.AUTH, authJson.toString(), new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                SPUtils.getInstance().put("name", jsonObject.optJSONObject("data").optString("name"),true);
                                SPUtils.getInstance().put("member_id", jsonObject.optJSONObject("data").optString("member_id"),true);
                                SPUtils.getInstance().put("card", jsonObject.optJSONObject("data").optString("card"),true);
                                ToastUtil.showToast(jsonObject.optString("message"));
                                EventBus.getDefault().post(new RfreshPersionInfos());
                                finish();
                            }else if (Constant.BACKLOGIN.equals(jsonObject.optString("status"))) {
                                JumpKillActivitys(LoginActivity.class);
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ("1".equals(isBackAuth)) {
                finish();
            } else {
                JumpActivitys(MainActivity.class);
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
